package cronjob

import (
	"context"
	"fmt"
	"github.com/jinzhu/copier"
	"go-zero-admin/application/job/rpc/pb"

	"go-zero-admin/application/job/api/internal/svc"
	"go-zero-admin/application/job/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateCronLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 创建定时任务
func NewCreateCronLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateCronLogic {
	return &CreateCronLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateCronLogic) CreateCron(req *types.CreateCronReq) (resp *types.CreateCronRes, err error) {
	var grpcReq pb.CreateCronInfo
	grpcReq.TaskName = req.TaskName
	grpcReq.MaxRetry = req.MaxRetry
	grpcReq.ExecCmd = req.ExecCmd
	grpcReq.TimeSchedule = req.TimeSchedule
	grpcReq.TaskType = req.TaskType
	res, err := l.svcCtx.JobCronSvc.CreateCron(l.ctx, &pb.CreateCronReq{CreateCronInfo: &grpcReq})
	if err != nil {
		return nil, err
	}
	fmt.Println(res, "resp", resp)
	taskInfo := res.TaskScheduleInfo
	resp = &types.CreateCronRes{}
	_ = copier.Copy(resp, taskInfo)
	return resp, nil
}
